package com.woniu.zsd.permission.service.impl;

import com.woniu.zsd.permission.entity.Resource;
import com.woniu.zsd.permission.mapper.ResourceMapper;
import com.woniu.zsd.permission.service.IResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.zsd.permission.utils.RoleResourceCacheUtil;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-27
 */
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements IResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    @Autowired
    private RoleResourceCacheUtil roleResourceCacheUtil;

    @Override
    public List<Resource> selResource() {
        return baseMapper.selectList(null);
    }

    @Override
    public int addResource(@Param("name") String name, @Param("resourceDesc") String resourceDesc) {
        if(name==null||resourceDesc==null) {
            return 0;
        }
        resourceMapper.addResource(name,resourceDesc);
        roleResourceCacheUtil.reloadRoleResourcesCache();
        return 1;
    }

    @Override
    public int updateResource(Integer resourceId,String name, String resourceDesc) {
        if(name==null||resourceDesc==null) {
            return 0;
        }
        resourceMapper.updateResource(resourceId,name,resourceDesc);
        roleResourceCacheUtil.reloadRoleResourcesCache();
        return 1;
    }

    @Override
    public int deleteResource(Integer resourceId) {
        if(resourceId==null)return 0;
        resourceMapper.deleteResource(resourceId);
        roleResourceCacheUtil.reloadRoleResourcesCache();
        return 1;
    }
}
